import { Battle } from "../../battle/Battle";
import { E_GameBaseAttr } from "../defs/com/E_GameBaseAttr";
import { E_PrayEffectType } from "../defs/lord/E_PrayEffectType";
import { CDPowerCalculation } from "../excel/CDPowerCalculation";
import { CDStage } from "../excel/CDStage";
import { CEMath } from "../excel_extend/CEMath";
import { BattleActorInitValue } from "../struct/BattleActorInitValue";
import { BattleInitValue } from "../struct/BattleInitValue";
import { Player } from "../struct/Player";
import { SDPray } from "./SDPray";
import { SDHuangXiaoxi } from "./SDHuangXiaoxi";


/**
 * 数据-战斗
 */
export abstract class SDBattle {


    /**
     * 从基础属性获取指定的属性值
     * @param type 
     * @param fromList 
     */
    public static getAttrByBaseAttrs(type: E_GameBaseAttr, fromList: number[]): number {
        let basev = fromList[type] || 0;
        let addv = 0;

        switch (type) {
            case E_GameBaseAttr.phyAtk:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.phyAtkPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.fireAtk:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.fireAtkPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.iceAtk:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.iceAtkPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.lightAtk:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.lightAtkPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.hp:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.hpPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.mana:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.manaPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.hpRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.hpRegenPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.manaRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.manaRegenPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.dribbleRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.dribbleRegenPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.dodgeRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.dodgeRegenPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.critRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.critRegenPercent, fromList) / 10000;
                break;
            case E_GameBaseAttr.blockRegen:
                addv = basev * this.getAttrByBaseAttrs(E_GameBaseAttr.blockRegenPercent, fromList) / 10000;
                break;
        }

        return Math.floor(basev + addv);
    }


    /**
     * 获取战力值
     * @param list 
     */
    public static getFightScore(list: number[]): number {
        if (!list || 0 === list.length) {
            return 0;
        }
        let score = 0;
        for (let i = 0, len = list.length; i < len; i++) {
            if (!list[i]) {
                continue;
            }
            const idx = CEMath.getExcelTypeByGameAttrType(i);
            const config = CDPowerCalculation.getData(idx);
            if (!config) {
                continue;
            }
            score += config.power * list[i];
        }
        return score;
    }


    /**
     * 需要杀的怪物数量
     * @param player 
     * @param config 
     * @returns 
     */
    public static getStageMonsterNum(player: Player, config: CDStage): number {
        let v = config.monsterNum;

        //祝福减少的关卡数量
        if (v > 0) {
            const percent = SDPray.getEffectByType(player, E_PrayEffectType.DelChapterMonsterNum);
            if (0 !== percent) {
                v -= Math.floor(percent * v / 100) || 1;
                if (v <= 0) {
                    v = 1;
                }
            }
        }
        //黄小西减少的关卡数量
        {
            const percent = SDHuangXiaoxi.getEffectByType(player, E_PrayEffectType.DelChapterMonsterNum);
            if (0 !== percent) {
                v -= Math.floor(percent * v / 100) || 1;
                if (v <= 0) {
                    v = 1;
                }
            }
        }

        return v;
    }


    /**
     * 获取战斗初始值
     * @param battle 
     * @returns 
     */
    public static getBattleInitValue(battle: Battle): BattleInitValue {
        const initv = BattleInitValue.create();
        const list = battle.actorList;

        for (let i = 0, len = list.length; i < len; i++) {
            const aiv = BattleActorInitValue.create();
            const id = list[i].actorId;
            initv.putInit(id, aiv);

            aiv.hp = list[i].hpMax.getV().floor().toNumber();
            aiv.mana = list[i].manaMax.getV().floor().toNumber();
        }

        return initv;
    }



}
